$(function () {
    var user = null, 
    html = [],
    thisPage = null;

    activateCoupon = function () {
        this.init();
    };
    activateCoupon.prototype = {
        init: function () {
            this.initData();
            this.bindEvent();
        },
        initData: function () {
          //判断是否登陆
          user = getLocalStorage(STORAGE_MANAGER.user);
          if (!user || !user.id) {
              redirect("../account/login.html");
              return false;
          }

        },
        bindEvent: function () {
            //后退
            $("#back").on("tap", function () {
                back();
            });
            //清除输入
            $("#clear").on("tap", function () {
                $("input").val("");
            });
            $("#card_no,#password").on("input", function () {

                if ("" == $("#card_no").val() ||
                    "" == $("#password").val()) {
                    $("#activenow").addClass("gray_color").unbind();
                } else {
                  $("#activenow").removeClass("gray_color").unbind().on("tap", function () {                      
                    //优惠券号，优惠券密码格式判断
                    if(!(/\d{12}/.test( $("#serial_no").val()))){
                      $.toast("卡号格式不对");
                      return false;
                    }
                    if(!(/[a-zA-Z0-9]{6}/.test( $("#password").val()))){
                      $.toast("密码格式不对");
                      return false;
                    }
                    var requestData = {
                      serial_no:$("#serial_no").val(),
                      password:$("#password").val(),
                      member_id:user.id,
                      org_id:0,
                      phone:user.name,
                    };
                    
                    //请求接口激活
                    $.ajax({
                    	type:"POST",
                    	url: CONFIG_MANAGER.RequstUrl + "v1/coupon/activate",
                    	async:true,
                    	data:requestData,
                    	headers:{ token:user.token }
                    }).done(function(data){
                      $.toast("激活成功", function(){
                        redirect("../mine/coupon.html");
                      });
                    }).fail(function(data){
                      $.toast(data.responseJSON.Message);
                    })
                    
                  });
                }
            });
        }
    };

    thisPage = new activateCoupon();
});
